package com.idea;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReplaceFileText {
	static String filePath_CN = "F:\\fanyi\\resources_cn\\messages\\";
	static String filePath_EN = "F:\\fanyi\\resources_en\\messages\\";
	static String outPath = "F:\\fanyi\\";

	public static void main(String[] args) {
		// 写入文件集合
		List<String> writeList = new ArrayList<String>();
		// 获取
		List<String> enFiles = ReplaceFileText.getFileName();
		for (String fileName : enFiles) {
			// 1.读取英文文件，存储为List<String>
			Map<String, String> enlist = ReplaceFileText.readFileByLines(filePath_EN + fileName);
			
			List<String> sortList = ReplaceFileText.readFileByLines2(filePath_EN + fileName);

			// 2.读取中文文件，存储为Map<String,String>
			Map<String, String> znlist = ReplaceFileText.readFileByLines(filePath_CN + fileName);

			// 3.遍历1，取2，生成文件
			for (String en : sortList) {
				String val = znlist.get(en);
				if (null != val) {
					writeList.add(en + "=" + val);
				}else{
					writeList.add(en + "=" + enlist.get(en));
				}
			}

			// 输出文件
			ReplaceFileText.writeFile(writeList, fileName);
		}

	}

	private static void writeFile(List<String> writeList, String fileName) {
		byte[] buff = new byte[] {};
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(outPath+fileName);
			for (String aa : writeList) {
				buff = aa.getBytes();
				out.write(buff, 0, buff.length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(null != out){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static Map<String, String> readFileByLines(String fileName) {
		Map<String, String> map = new HashMap<String, String>();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一行");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读一行，读入null时文件结束
			while ((tempString = reader.readLine()) != null) {

				String[] str = tempString.split("=");
				if(str.length >= 2 && !tempString.contains("# ")){
					map.put(str[0], str[1]+"\n");
				}

				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return map;
	}

	public static List<String> readFileByLines2(String fileName) {
		List<String> list = new ArrayList<String>();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一行");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读一行，读入null时文件结束
			while ((tempString = reader.readLine()) != null) {

				list.add(tempString.split("=")[0]);

				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return list;
	}

	public static List<String> getFileName() {
		List<String> files = new ArrayList<String>();
		File f = new File(filePath_EN);
		if (!f.exists()) {
			System.out.println(filePath_EN + " not exists");
			return null;
		}

		File fa[] = f.listFiles();
		for (int i = 0; i < fa.length; i++) {
			File fs = fa[i];
			if (fs.isDirectory()) {
				System.out.println(fs.getName() + " [目录]");
			} else {
				if (fs.getName().contains("properties")) {
					files.add(fs.getName());
				} else {
					System.out.println(fs.getName() + " [文件类型不匹配]");
				}
			}
		}

		return files;
	}

}
